<template>
  <div class="role">
    <layout-header />
    <div class="role-main">
      <div class="role-container">
        <h1 class="role__title">
          {{ userStore.isVisitor ? '请选择注册身份' : '请选择登录身份' }}
        </h1>
        <el-radio-group class="role-list" v-model="selRole">
          <el-radio
            v-for="role in roleList"
            :key="role.roleId"
            class="role-item"
            :value="role.roleName"
          >
            <div class="role-item-main">
              <div class="role-item-icon" v-if="role.icon">
                <svg-icon :icon-class="role.icon" />
              </div>
              <p>{{ role.roleTitle }}</p>
            </div>
          </el-radio>
        </el-radio-group>
        <div class="role-footer">
          <el-button type="primary" class="role-next" @click="next">下一步</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import LayoutHeader from '@/components/Layout/header.vue'
import useUserStore from '@/stores/user'
import { Role_Center_Host, Role_Enum } from '@/common/enum'
import { reactive } from 'vue'
import { ElLoading, ElMessage } from 'element-plus'
import api from '@/http/index'
import { useRouter } from 'vue-router'
const userStore = useUserStore()
const router = useRouter()
const Role_Icon = {
  enterprise_boss: 'company',
  enterprise_driver: 'car',
  enterprise_branch: 'branch',
  enterprise_admin: 'man-admin',
}

const selRole = ref()

let roleList = reactive([])
const getRoleList = async () => {
  await userStore.getAllRoles()
  const filterRoles = storeToRefs(userStore)
    .allRoles.value.filter(
      (item) => ![Role_Enum.sys_admin, Role_Enum.sys_visitor].includes(item.roleId),
    )
    .map((item) => ({
      ...item,
      icon: Role_Icon?.[item.roleName],
    }))
  if (userStore.isVisitor) {
    filterRoles.forEach((role) => roleList.push(role))
  } else {
    filterRoles.forEach((role) => userStore.roleIds.includes(role.roleId) && roleList.push(role))
  }
}
getRoleList()

const next = () => {
  if (!selRole?.value) return ElMessage.error('请选择身份')
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0, 0, 0, 0.7)',
  })
  if (!userStore.isVisitor) {
    const centerHost = Role_Center_Host[selRole.value]
    centerHost && (location.href = centerHost)
  }
  const submitData = {
    roleIds: [Role_Enum[selRole.value]],
    userId: userStore.user.userId,
  }
  api.user
    .bindRole(submitData)
    .then(async () => {
      await userStore.getUserInfo()
      const sucess = !submitData.roleIds.some((roleId) => !userStore.roleIds.includes(roleId))
      if (!sucess) return ElMessage.error('身份绑定失败, 请稍后再试')
      ElMessage.success('身份绑定成功')
      // 企业创始人 且 未创建企业
      if (userStore.checkRole(Role_Enum.enterprise_boss) && !userStore.user?.enterpriseId) {
        router.push('/create')
        return
      }
      router.push(`/auth/bind?loginRole=${selRole.value}`)
    })
    .finally(() => {
      loading.close()
    })
}
</script>

<style lang="scss">
.role {
  width: 100%;
  background-color: #f5f5f5;
  height: 100vh;
  overflow-y: scroll;
  &-main {
    width: 100%;
    min-height: calc(100vh - 76px);
    padding: 28px 0;
  }
  &-container {
    position: relative;
    width: 928px;
    margin: 0 auto;
    background-color: #fff;
    box-shadow:
      0 12px 32px 12px rgba(0, 0, 0, 0.03),
      0 9px 24px 0 rgba(0, 0, 0, 0.05),
      0 6px 16px -8px rgba(0, 0, 0, 0.08),
      0 2px 8px -4px rgba(32, 97, 230, 0.1);
  }
  &__title {
    padding: 20px;
    color: #262626;
  }
  &-list {
    width: 100%;
    display: flex;
    flex-direction: column;
  }
  &-item {
    width: 100%;
    height: 126px;
    margin: 0;
    padding: 0 40px;
    border: 2px solid transparent;
    transition: all 0.3s;
    &:hover {
      border-color: var(--el-color-primary);
      box-shadow:
        0 5px 12px 4px rgba(0, 0, 0, 0.09),
        0 3px 6px 0 rgba(0, 0, 0, 0.12),
        0 1px 2px -2px rgba(0, 0, 0, 0.16);
    }
    &:hover .el-radio__inner {
      border-color: var(--el-color-primary);
    }
    &.is-checked {
      border-color: var(--el-color-primary);
      background-color: var(--el-color-primary-light-8);
    }
    &-main {
      display: flex;
      align-items: center;
      margin-left: 18px;
      > p {
        color: rgba(0, 0, 0, 0.85);
        font-size: 20px;
        font-weight: 500;
      }
    }
    &-icon {
      line-height: 44px;
      text-align: center;
      font-size: 24px;
      width: 44px;
      height: 44px;
      background-color: var(--el-color-primary-light-9);
      border-radius: 50%;
      margin-right: 26px;
    }
    &:last-child .el-radio__label {
      &::after {
        display: none;
      }
    }
    .el-radio__label {
      flex: 1;
      height: 100%;
      display: flex;
      align-items: center;
      position: relative;
      &::after {
        content: '';
        position: absolute;
        left: 50%;
        bottom: -2px;
        transform: translateX(-50%);
        width: 100%;
        height: 1px;
        background-color: rgba(0, 0, 0, 0.15);
      }
    }
    .el-radio__inner {
      width: 26px;
      height: 26px;
    }
  }
  &-footer {
    position: sticky;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 12px 24px;
    height: 64px;
    background: #fff;
  }
  &-next {
    width: 100%;
    height: 100%;
    font-size: 16px;
  }
}
</style>
